package com.shigu.seller.service;

import com.opentae.data.mall.beans.MallSeller;
import com.opentae.data.mall.examples.MallSellerExample;
import com.opentae.data.mall.interfaces.MallSellerMapper;
import com.shigu.MallResponse;
import com.shigu.PageResponse;
import com.shigu.exceptions.AdminException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SellerAdminService {
    @Autowired
    MallSellerMapper mallSellerMapper;

    /**
     *  添加商家
     * @author 郭尔
     * @date 2019/1/12 11:03
     */
    public MallResponse addSeller(String sellerName) throws AdminException {
        if (StringUtils.isBlank(sellerName)) {
            throw new AdminException("商家名称不能为空");
        }
        if (mallSellerMapper.checkSameName(sellerName)>0) {
            throw new AdminException("已存在该商家名称");
        }
        MallSeller seller = new MallSeller();
        seller.setSellerName(sellerName);
        mallSellerMapper.insertSelective(seller);
        return new MallResponse().success();
    }

    /**
     * 商家列表
     * @author 郭尔
     * @date 2019/1/12 11:06
     */
    public PageResponse<MallSeller> sellerList(int page,int size){
        PageResponse<MallSeller> response = new PageResponse<>();
        MallSellerExample example = new MallSellerExample();

        example.setStartIndex( (page-1)*size);
        example.setEndIndex(size);
        example.setOrderByClause("seller_id DESC");
        response.setData(mallSellerMapper.selectByConditionList(example));
        response.setTotal(mallSellerMapper.countByExample(example));
        return response;
    }

    /**
     *
     * 更改商家名称
     * @author 郭尔
     * @date 2019/1/12 11:16
     */
    public MallResponse modifySellerName(Long sellerId,String sellerName) throws AdminException {
        if (StringUtils.isBlank(sellerName)) {
            throw new AdminException("商家名称不能为空");
        }
        if (mallSellerMapper.checkSameName(sellerName)>0) {
            throw new AdminException("已存在该商家名称");
        }
        MallSeller seller = new MallSeller();
        seller.setSellerId(sellerId);
        seller.setSellerName(sellerName);
        mallSellerMapper.updateByPrimaryKeySelective(seller);
        return new MallResponse().success();
    }

    /**
     *
     * @description 删除商家
     * @author 郭尔
     * @date 2019/1/14 14:23
     */
    public MallResponse deleteSeller(Long sellerId){
        mallSellerMapper.deleteByPrimaryKey(sellerId);
        return new MallResponse().success();
    }
    /**
     *
     * @description 商家
     * @author 郭尔
     * @date 2019/1/14 14:23
     */
    public PageResponse<MallSeller> sellers(){
        PageResponse<MallSeller> response = new PageResponse<>();
        MallSellerExample example = new MallSellerExample();
        response.setData(mallSellerMapper.selectByExample(example));
        return response;
    }
}
